package com.fy.oa.zc.manager;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import com.fy.oa.zc.domain.FySummaryView;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.v3x.services.flow.FlowFactory;

public class FySummaryViewManagerImpl implements FySummaryViewManager {
	private static final Log logger = LogFactory.getLog(FySummaryViewManagerImpl.class);
	private JdbcTemplate jdbcTemplate;
	private FlowFactory flowFactory;

	public void setFlowFactory(FlowFactory flowFactory) {
		this.flowFactory = flowFactory;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public long getFlowState(long summaryid) {
		try {
			if (flowFactory == null)
				flowFactory = (FlowFactory) AppContext.getBean("flowFactory");
			long state = flowFactory.getFlowState("", summaryid);
			return state;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -2L;
	}
//new Object[] { Long.valueOf(summaryid) },
	public FySummaryView getFySummaryView(long summaryid) {
		try {
			FySummaryView view = jdbcTemplate.queryForObject(
					"select s.id summaryid,s.start_member_id startMember,t.templete_number templateCode,s.state state from col_summary s left join ctp_template t on s.templete_id = t.id where  t.IS_SYSTEM = 1 and s.id = ?",
					 new Object[] { Long.valueOf(summaryid) },
					ParameterizedBeanPropertyRowMapper.newInstance(FySummaryView.class));
			return view;
		} catch (Exception e) {
			System.out.println("异常表单summaryID" + summaryid);
			e.printStackTrace();
			logger.error("异常表单summaryID"+summaryid, e);
		}
		return null;
	}

	@Override
	public boolean isInform(long affairID) {
		Map<String, Object> result = jdbcTemplate
				.queryForMap("select c.NODE_POLICY NODE_POLICY from ctp_affair c where c.id=?", affairID);
		return "inform".equals(result.get("NODE_POLICY"));
	}
}
